package com.kye.express.business.invoice_online.detail;

import android.content.Context;
import android.text.TextUtils;

import com.kye.express.R;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.RxView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.invoice_online.MailInvoiceResultBean;
import com.kye.model.bean.invoice_online.RequestGetInvoiceDetailBean;
import com.kye.model.bean.invoice_online.RequestMailInvoiceBean;
import com.kye.model.bean.user.User;
import com.kye.model.download.DownLoadObserver;
import com.kye.model.download.DownloadInfo;
import com.kye.model.download.DownloadManager;
import com.kye.model.service.InvoiceOnlineService;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface ElectroniclnvoiceFragmentContract {

    interface ElectroniclnvoiceFragmentView extends RxView {

        void onDownloadComplete();

        void onDownloading(DownloadInfo value);

        void onDownloadError(String msg);

        void onMailComplete();

        void onMailLoading();

        void onMailLoaded(MailInvoiceResultBean datas);

        void onMailApiError(String msg);

        void onMailOtherError(String msg);

    }

    class ElectroniclnvoiceFragmentViewPresenter extends BasePresenter<ElectroniclnvoiceFragmentContract.ElectroniclnvoiceFragmentView> {

        private Context mContext;

        public ElectroniclnvoiceFragmentViewPresenter(Context context) {
            this.mContext = context;
        }

        /**
         * 下载发票
         */
        public void downloadInvoice(String url, String fileName) {
            DownloadManager.getInstance().download(url, fileName, new DownLoadObserver() {
                @Override
                public void onNext(DownloadInfo value) {
                    super.onNext(value);
                    if (value != null) {
                        getView().onDownloading(value);
                    }
                }

                @Override
                public void onComplete() {
                    if (downloadInfo != null) {
                        getView().onDownloadComplete();
                    }
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                    e.printStackTrace();
                    getView().onDownloadError(mContext.getString(R.string.invoice_online_download_failed));
                }
            });
        }


        public void mailInvoice(String sourceCode, String email) {
            String phone = "";
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
            }

            RetrofitManager.getInstance().createOpenApiService(InvoiceOnlineService.class)
                    .mailInvoice(new RequestMailInvoiceBean(phone, sourceCode, email))
                    .compose(getView().<OpenApiResult<MailInvoiceResultBean>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<MailInvoiceResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onMailLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onMailOtherError(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onLoaded() {
                            if (getView() != null) {
                                getView().onMailComplete();
                            }
                        }

                        @Override
                        public void onSuccess(OpenApiResult<MailInvoiceResultBean> result, MailInvoiceResultBean datas) {
                            if (datas != null) {
                                getView().onMailLoaded(datas);
                            }
                        }

                        @Override
                        public void onApiError(OpenApiResult<MailInvoiceResultBean> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onMailApiError(msg);
                            } else {
                                getView().onMailOtherError(mContext.getString(R.string.common_net_error));
                            }
                        }

                    });
        }
    }
}
